package com.pingtai.indent.action;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import com.pingtai.filter.ChildUserFilter;
import com.pingtai.indent.entity.Qq;
import com.pingtai.indent.entity.TencentBusinessRenzheng;
import com.pingtai.indent.entity.TencentPersonRenzheng;
import com.pingtai.indent.entity.TencentWeixinFanStatistics;
import com.pingtai.indent.entity.UserBaseIndentType;
import com.pingtai.indent.entity.UserIndent;
import com.pingtai.indent.entity.UserIndentMoneyTmp;
import com.pingtai.indent.entity.UserIndentSearch;
import com.pingtai.indent.entity.UserIndentType;
import com.pingtai.indent.entity.XinlangPrimaryFanStatistics;
import com.pingtai.indent.service.WeiboIndentService;
import com.pingtai.user.entity.User;
import com.pingtai.user.entity.UserPay;
import com.pingtai.user.service.UserManagerService;
import com.pingtai.util.PaginationSupport;
import com.pingtai.util.Utils;

public class TencentIndentAction {
	
	private UserBaseIndentType baseIndentType;
	private WeiboIndentService weiboIndentService;
	private String base;
	private UserManagerService userManagerService;
	private User curUser; 
	private Integer currentPage=1;
	private Integer pageNum;
	private PaginationSupport paginationSupport;
	private String current;
	private UserIndentSearch qe;
	private String queryString;
	private Float price;
	private UserIndent userIndent;
private String typeRadio;
	
	public String getTypeRadio() {
		return typeRadio;
	}
	public void setTypeRadio(String typeRadio) {
		this.typeRadio = typeRadio;
	}
	
	public UserIndent getUserIndent() {
		return userIndent;
	}

	public void setUserIndent(UserIndent userIndent) {
		this.userIndent = userIndent;
	}

	public Float getPrice() {
		return price;
	}

	public void setPrice(Float price) {
		this.price = price;
	}

	public String getQueryString() {
		return queryString;
	}

	public void setQueryString(String queryString) {
		this.queryString = queryString;
	}

	public UserBaseIndentType getBaseIndentType() {
		return baseIndentType;
	}

	public void setBaseIndentType(UserBaseIndentType baseIndentType) {
		this.baseIndentType = baseIndentType;
	}

	public WeiboIndentService getWeiboIndentService() {
		return weiboIndentService;
	}

	public void setWeiboIndentService(WeiboIndentService weiboIndentService) {
		this.weiboIndentService = weiboIndentService;
	}

	public String getBase() {
		return base;
	}

	public void setBase(String base) {
		this.base = base;
	}

	public UserManagerService getUserManagerService() {
		return userManagerService;
	}

	public void setUserManagerService(UserManagerService userManagerService) {
		this.userManagerService = userManagerService;
	}

	public User getCurUser() {
		return curUser;
	}

	public void setCurUser(User curUser) {
		this.curUser = curUser;
	}

	public Integer getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(Integer currentPage) {
		this.currentPage = currentPage;
	}

	public Integer getPageNum() {
		return pageNum;
	}

	public void setPageNum(Integer pageNum) {
		this.pageNum = pageNum;
	}

	public PaginationSupport getPaginationSupport() {
		return paginationSupport;
	}

	public void setPaginationSupport(PaginationSupport paginationSupport) {
		this.paginationSupport = paginationSupport;
	}

	public String getCurrent() {
		return current;
	}

	public void setCurrent(String current) {
		this.current = current;
	}

	public UserIndentSearch getQe() {
		return qe;
	}

	public void setQe(UserIndentSearch qe) {
		this.qe = qe;
	}

	public String tencentManageUIForQqFan(){
		baseIndentType =weiboIndentService.getBaseIndentTypeById(new Integer(base)); 
		
		curUser = userManagerService.findCurrectUser();
		if(currentPage>0) {
			pageNum = currentPage;
			currentPage = PaginationSupport.PAGESIZE*(currentPage-1);
		}
		
		List baseList = new ArrayList();
		Set childBaseIndentType = baseIndentType.getChildrenBaseIndentType();
		Iterator childIt = childBaseIndentType.iterator();
		while(childIt.hasNext()){
			UserBaseIndentType ubt = (UserBaseIndentType) childIt.next();
			baseList.add(ubt.getId());
			
			Set childBaseIndentType2 = ubt.getChildrenBaseIndentType();
			Iterator childIt2 = childBaseIndentType2.iterator();
			while(childIt2.hasNext()){
				UserBaseIndentType ubt2 = (UserBaseIndentType) childIt2.next();
				baseList.add(ubt2.getId());
			}
			
		}
		baseList.add(new Integer(base));
		
		if(userManagerService.isAudit(curUser)){
			List produceList = userManagerService.getProduceList2();
			int len = baseList.size();
			int i=0;
			for(;i<len;i++){
				Integer b =  (Integer) baseList.get(i);
				boolean f = false;
				for(int j=0;j<produceList.size();j++){
					UserIndentType u = (UserIndentType) produceList.get(j);
					UserBaseIndentType b2 = u.getUserBaseIndentType();
					if(b.equals(b2.getId())){
						f = true;
						break;
					}
				}
				
				if(!f){
					baseList.remove(i);
					i--;
					len--;
				}
			}
		}
		
		if(userManagerService.isNotUser(curUser)&& !userManagerService.isUser_2(curUser)){
			paginationSupport = weiboIndentService.getIndentForPage(currentPage,true,baseList);
		}else{
			paginationSupport = weiboIndentService.getIndentForPage(currentPage,false,baseList);
		}
		if(qe!=null){
			current = qe.getStatus();
		}else{
			current = "all";
		}
		
	    
		return "success";
	}
	
	public String tencentManageUIForSearch_qq_fan(){
		baseIndentType =weiboIndentService.getBaseIndentTypeById(new Integer(base)); 
		curUser = userManagerService.findCurrectUser();
		if(currentPage>0) {
			pageNum = currentPage;
			currentPage = PaginationSupport.PAGESIZE*(currentPage-1);
		}
		
		if(queryString!=null&&!queryString.isEmpty()){
			try {
				qe = weiboIndentService.paseQueryString(qe,queryString);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		List baseList = new ArrayList();
		Set childBaseIndentType = baseIndentType.getChildrenBaseIndentType();
		Iterator childIt = childBaseIndentType.iterator();
		while(childIt.hasNext()){
			UserBaseIndentType ubt = (UserBaseIndentType) childIt.next();
			baseList.add(ubt.getId());
			Set childBaseIndentType2 = ubt.getChildrenBaseIndentType();
			Iterator childIt2 = childBaseIndentType2.iterator();
			while(childIt2.hasNext()){
				UserBaseIndentType ubt2 = (UserBaseIndentType) childIt2.next();
				baseList.add(ubt2.getId());
			}
		}
		baseList.add(new Integer(base));
		
		if(userManagerService.isAudit(curUser)){
			List produceList = userManagerService.getProduceList2();
			int len = baseList.size();
			int i=0;
			for(;i<len;i++){
				Integer b =  (Integer) baseList.get(i);
				boolean f = false;
				for(int j=0;j<produceList.size();j++){
					UserIndentType u = (UserIndentType) produceList.get(j);
					UserBaseIndentType b2 = u.getUserBaseIndentType();
					if(b.equals(b2.getId())){
						f = true;
						break;
					}
				}
				
				if(!f){
					baseList.remove(i);
					i--;
					len--;
				}
			}
		}
		
		if(userManagerService.isNotUser(curUser)&& !userManagerService.isUser_2(curUser)){
			paginationSupport = weiboIndentService.getIndentForPage(currentPage,true,baseList,qe);
		}else{
			paginationSupport = weiboIndentService.getIndentForPage(currentPage,false,baseList,qe);
		}
		if(qe!=null){
			current = qe.getStatus();
		}else{
			current = "all";
		}
		return "success";
	}
	
	public String toTencentAddForQqFan() {
		if(ChildUserFilter.getFlag2()==0) {
			return "produceWeihu";
    	}
		baseIndentType = weiboIndentService.getBaseIndentTypeById(new Integer(
				base));

		if (baseIndentType.getEnable() == 0) {
			return "produceWeihu";
		}

		curUser = userManagerService.findCurrectUser();
		if (userManagerService.isRoot(curUser)) {
			price = baseIndentType.getPrice();
		} else {
			UserIndentType indentType = weiboIndentService
					.getUserIndentTypeByUserAndType(curUser,
							baseIndentType.getId());
			if (indentType == null) {
				return "403";
			} else {
				indentType = weiboIndentService.updateIndentType(indentType);
				price = indentType.getPrice();
			}
		}
		return "success";
	}
	
	public String tencentAddForQqFan_dq() throws ParseException{
		//判断价钱是否合法
		User curUser = userManagerService.findCurrectUser();
		boolean flag = weiboIndentService.isLawfulOfPrice(price,curUser,new Integer(base));
		if(flag){
			
			userIndent.setCreateDate(new Date());
			//userIndent.setNowNumber(userIndent.getStartNumber());
			User parentUser = userManagerService.findUserById(curUser.getParentUserId());
			userIndent.setParentUser(parentUser);
			userIndent.setState(0);
			
			baseIndentType =weiboIndentService.getBaseIndentTypeById(new Integer(base));
			if(userManagerService.isRoot(curUser)){
				price = baseIndentType.getPrice();
			}else{
				UserIndentType indentType = weiboIndentService.getUserIndentTypeByUserAndType(curUser,baseIndentType.getId());
				if(indentType==null){
					return "403";
				}else{
					Float oldPrice = indentType.getPrice();
					indentType = weiboIndentService.updateIndentType(indentType);
					price = indentType.getPrice();
					if(!oldPrice.equals(price)){
						return "priceChange";
					}
				}
			}
			
			userIndent.setType(new Integer(base));
			userIndent.setUser(curUser);
			Float t = price*userIndent.getNumber()/baseIndentType.getUnit2();
			userIndent.setTotalPrice(t);
			userIndent.setPrice(price);
			//判余额是否充足
			Float money = curUser.getMoney();
			Float money2 = (float) Math.round(money*100/100);
			if(money2<t){
				return "moneyLow";
			}
			userIndent.setBasePrice(baseIndentType.getPrice());
			Float baseTotalPrice = baseIndentType.getPrice()*userIndent.getNumber()/baseIndentType.getUnit2();
			userIndent.setBaseTotalPrice(baseTotalPrice);
			userIndent.setBaseIndentType(baseIndentType);
			userIndent.setWeixinNum(userIndent.getQq().getWeixin());
			userIndent.setRenzheng(null);
			
			//判断此微信号是否刷过
			Integer num = weiboIndentService.getWeixinSanmeNum(userIndent.getQq().getWeixin());
			userIndent.setWeixinSameNum(num);
			//保存订单表
			userIndent = weiboIndentService.saveUserIndent(userIndent);
			
			Qq q = userIndent.getQq();
			q.setUserIndent(userIndent);
			q = (Qq) weiboIndentService.saveObject(q);
			
			//保存腾讯微博订单对应QQ号码
			
	// weiboIndentService.saveIndentQq(qq,userIndent);
			
			//所需金额保存到临时表
			UserIndentMoneyTmp userIndentMoneyTmp = new UserIndentMoneyTmp();
			userIndentMoneyTmp.setMoney(t);
			userIndentMoneyTmp.setUser(curUser);
			userIndentMoneyTmp.setUserIndent(userIndent);
			userIndentMoneyTmp = weiboIndentService.saveUserIndentMoneyTmp(userIndentMoneyTmp);
			//从用户账户中的总金额减去订单总价 保存
			money = curUser.getMoney();
			money = money -t;
			curUser.setMoney(money);
			UserPay userPay = userManagerService.saveUserMoneyForIndent(curUser,0,t,userIndent);
			
			weiboIndentService.getIndentDao().evict(userIndent);
			weiboIndentService.getIndentDao().evict(userIndentMoneyTmp);
			weiboIndentService.getIndentDao().evict(userPay);
			
			
			//保存返利关系
			UserIndentType indentType = weiboIndentService.getUserIndentTypeByUserAndType(curUser,baseIndentType.getId());
			weiboIndentService.saveUserIndentRebateRelation(userIndent,baseIndentType,0,indentType,curUser,price,null,null);
			//userIndent2 = userIndent;
			userIndent=null;
			
		}else{
			return "add_indent_403";
		}
	
	return "success";
	}
	
	public String tencentAddForQqFan() throws ParseException{
		//判断价钱是否合法
				User curUser = userManagerService.findCurrectUser();
				boolean flag = weiboIndentService.isLawfulOfPrice(price,curUser,new Integer(base));
				if(flag){
					
					userIndent.setCreateDate(new Date());
					//userIndent.setNowNumber(userIndent.getStartNumber());
					User parentUser = userManagerService.findUserById(curUser.getParentUserId());
					userIndent.setParentUser(parentUser);
					userIndent.setState(0);
					
					baseIndentType =weiboIndentService.getBaseIndentTypeById(new Integer(base));
					if(userManagerService.isRoot(curUser)){
						price = baseIndentType.getPrice();
					}else{
						UserIndentType indentType = weiboIndentService.getUserIndentTypeByUserAndType(curUser,baseIndentType.getId());
						if(indentType==null){
							return "403";
						}else{
							Float oldPrice = indentType.getPrice();
							indentType = weiboIndentService.updateIndentType(indentType);
							price = indentType.getPrice();
							if(!oldPrice.equals(price)){
								return "priceChange";
							}
						}
					}
					
					userIndent.setType(new Integer(base));
					userIndent.setUser(curUser);
					Float t = price*userIndent.getNumber()/baseIndentType.getUnit2();
					userIndent.setTotalPrice(t);
					userIndent.setPrice(price);
					//判余额是否充足
					Float money = curUser.getMoney();
					Float money2 = (float) Math.round(money*100/100);
					if(money2<t){
						return "moneyLow";
					}
					userIndent.setBasePrice(baseIndentType.getPrice());
					Float baseTotalPrice = baseIndentType.getPrice()*userIndent.getNumber()/baseIndentType.getUnit2();
					userIndent.setBaseTotalPrice(baseTotalPrice);
					userIndent.setBaseIndentType(baseIndentType);
					userIndent.setWeixinNum(userIndent.getQq().getWeixin());
					userIndent.setRenzheng(null);
					
					//判断此微信号是否刷过
					Integer num = weiboIndentService.getWeixinSanmeNum(userIndent.getQq().getWeixin());
					userIndent.setWeixinSameNum(num);
					//保存订单表
					userIndent = weiboIndentService.saveUserIndent(userIndent);
					
					Qq q = userIndent.getQq();
					q.setUserIndent(userIndent);
					q = (Qq) weiboIndentService.saveObject(q);
					
					//保存腾讯微博订单对应QQ号码
					
			// weiboIndentService.saveIndentQq(qq,userIndent);
					
					//所需金额保存到临时表
					UserIndentMoneyTmp userIndentMoneyTmp = new UserIndentMoneyTmp();
					userIndentMoneyTmp.setMoney(t);
					userIndentMoneyTmp.setUser(curUser);
					userIndentMoneyTmp.setUserIndent(userIndent);
					userIndentMoneyTmp = weiboIndentService.saveUserIndentMoneyTmp(userIndentMoneyTmp);
					//从用户账户中的总金额减去订单总价 保存
					money = curUser.getMoney();
					money = money -t;
					curUser.setMoney(money);
					UserPay userPay = userManagerService.saveUserMoneyForIndent(curUser,0,t,userIndent);
					
					weiboIndentService.getIndentDao().evict(userIndent);
					weiboIndentService.getIndentDao().evict(userIndentMoneyTmp);
					weiboIndentService.getIndentDao().evict(userPay);
					
					
					//保存返利关系
					UserIndentType indentType = weiboIndentService.getUserIndentTypeByUserAndType(curUser,baseIndentType.getId());
					weiboIndentService.saveUserIndentRebateRelation(userIndent,baseIndentType,0,indentType,curUser,price,null,null);
					//userIndent2 = userIndent;
					userIndent=null;
					
				}else{
					return "add_indent_403";
				}
			
			return "success";
	}
	
	public String tencentPersonRenzhengManageUI(){
		baseIndentType =weiboIndentService.getBaseIndentTypeById(new Integer(base)); 
		
		curUser = userManagerService.findCurrectUser();
		if(currentPage>0) {
			pageNum = currentPage;
			currentPage = PaginationSupport.PAGESIZE*(currentPage-1);
		}
		if(userManagerService.isNotUser(curUser)&& !userManagerService.isUser_2(curUser)){
			paginationSupport = weiboIndentService.getIndentForPage(currentPage,true,new Integer(base));
		}else{
			paginationSupport = weiboIndentService.getIndentForPage(currentPage,false,new Integer(base));
		}
		if(qe!=null){
			current = qe.getStatus();
		}else{
			current = "all";
		}
	    
		return "success";
	}
	
	public String toTencentPersonRenzhengAdd(){
		if(ChildUserFilter.getFlag2()==0) {
			return "produceWeihu";
    	}
		baseIndentType = weiboIndentService.getBaseIndentTypeById(new Integer(
				base));

		if (baseIndentType.getEnable() == 0) {
			return "produceWeihu";
		}

		curUser = userManagerService.findCurrectUser();
		if (userManagerService.isRoot(curUser)) {
			price = baseIndentType.getPrice();
		} else {
			UserIndentType indentType = weiboIndentService
					.getUserIndentTypeByUserAndType(curUser,
							baseIndentType.getId());
			if (indentType == null) {
				return "403";
			} else {
				indentType = weiboIndentService.updateIndentType(indentType);
				price = indentType.getPrice();
			}
		}
		return "success";
	}
	
	public String tencentPersonRenzhengAdd(){
		//判断价钱是否合法
		User curUser = userManagerService.findCurrectUser();
		boolean flag = weiboIndentService.isLawfulOfPrice(price,curUser,new Integer(base));
		if(flag){
			
			userIndent.setCreateDate(new Date());
			//userIndent.setNowNumber(userIndent.getStartNumber());
			User parentUser = userManagerService.findUserById(curUser.getParentUserId());
			userIndent.setParentUser(parentUser);
			userIndent.setState(0);
			
			baseIndentType =weiboIndentService.getBaseIndentTypeById(new Integer(base));
			if(userManagerService.isRoot(curUser)){
				price = baseIndentType.getPrice();
			}else{
				UserIndentType indentType = weiboIndentService.getUserIndentTypeByUserAndType(curUser,baseIndentType.getId());
				if(indentType==null){
					return "403";
				}else{
					Float oldPrice = indentType.getPrice();
					indentType = weiboIndentService.updateIndentType(indentType);
					price = indentType.getPrice();
					if(!oldPrice.equals(price)){
						return "priceChange";
					}
				}
			}
			
			userIndent.setType(new Integer(base));
			userIndent.setUser(curUser);
			Float t = price*1/baseIndentType.getUnit2();
			userIndent.setTotalPrice(t);
			userIndent.setPrice(price);
			//判余额是否充足
			Float money = curUser.getMoney();
			Float money2 = (float) Math.round(money*100/100);
			if(money2<t){
				return "moneyLow";
			}
			userIndent.setBasePrice(baseIndentType.getPrice());
			Float baseTotalPrice = baseIndentType.getPrice()*1/baseIndentType.getUnit2();
			userIndent.setBaseTotalPrice(baseTotalPrice);
			
			userIndent.setBaseIndentType(baseIndentType);
			
			TencentPersonRenzheng tencentPersonRenzheng = userIndent.getRenzheng().getTencentPersonRenzheng();
			tencentPersonRenzheng.setState(userIndent.getState());
			
			String remark = tencentPersonRenzheng.getRemark();
			remark = Utils.replaceEnter(remark);
			tencentPersonRenzheng.setRemark(remark);
			
			weiboIndentService.saveObject(tencentPersonRenzheng);
			weiboIndentService.saveObject(userIndent.getRenzheng());
			userIndent.setNumber(1);
			//保存订单表
			userIndent = weiboIndentService.saveUserIndent(userIndent);
			
			// weiboIndentService.saveIndentQq(qq,userIndent);
			
			//所需金额保存到临时表
			UserIndentMoneyTmp userIndentMoneyTmp = new UserIndentMoneyTmp();
			userIndentMoneyTmp.setMoney(t);
			userIndentMoneyTmp.setUser(curUser);
			userIndentMoneyTmp.setUserIndent(userIndent);
			userIndentMoneyTmp = weiboIndentService.saveUserIndentMoneyTmp(userIndentMoneyTmp);
			//从用户账户中的总金额减去订单总价 保存
			money = curUser.getMoney();
			money = money -t;
			curUser.setMoney(money);
			UserPay userPay = userManagerService.saveUserMoneyForIndent(curUser,0,t,userIndent);
			
			weiboIndentService.getIndentDao().evict(userIndent);
			weiboIndentService.getIndentDao().evict(userIndentMoneyTmp);
			weiboIndentService.getIndentDao().evict(userPay);
			
			
			//保存返利关系
			UserIndentType indentType = weiboIndentService.getUserIndentTypeByUserAndType(curUser,baseIndentType.getId());
			weiboIndentService.saveUserIndentRebateRelation(userIndent,baseIndentType,0,indentType,curUser,price,null,null);
			//userIndent2 = userIndent;
			userIndent=null;
			
		}else{
			return "add_indent_403";
		}
	
	return "success";
	}
	
	public String tencentPersonRenzhengManageUIForSearch(){
		baseIndentType =weiboIndentService.getBaseIndentTypeById(new Integer(base)); 
		curUser = userManagerService.findCurrectUser();
		if(currentPage>0) {
			pageNum = currentPage;
			currentPage = PaginationSupport.PAGESIZE*(currentPage-1);
		}
		
		if(queryString!=null&&!queryString.isEmpty()){
			try {
				qe = weiboIndentService.paseQueryString(qe,queryString);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(userManagerService.isNotUser(curUser)&& !userManagerService.isUser_2(curUser)){
			paginationSupport = weiboIndentService.getIndentForPage(currentPage,true,new Integer(base),qe,"tencentPersonRenzheng");
		}else{
			paginationSupport = weiboIndentService.getIndentForPage(currentPage,false,new Integer(base),qe,"tencentPersonRenzheng");
		}
		if(qe!=null){
			current = qe.getStatus();
		}else{
			current = "all";
		}
		return "success";
	}
	
	public String toTencentRenzhengIndentRefund(){
		userIndent = weiboIndentService.getUserIndentById(userIndent.getId());
		baseIndentType =userIndent.getBaseIndentType();
		return "success";
	}
	
	public String toTencentRenzhengIndentDetail(){
		userIndent = weiboIndentService.getUserIndentById(userIndent.getId());
		baseIndentType =userIndent.getBaseIndentType();
		return "success";
	}
	
	public String toTencentRenzhengIndentModify(){
		userIndent = weiboIndentService.getUserIndentById(userIndent.getId());
		baseIndentType =userIndent.getBaseIndentType();
		return "success";
	}
	
	public String toTencentRenzhengIndentComplete(){
		userIndent = weiboIndentService.getUserIndentById(userIndent.getId());
		baseIndentType =userIndent.getBaseIndentType();
		return "success";
	}
	
	public String tencentRenzhengIndentComplete(){
		baseIndentType =weiboIndentService.getBaseIndentTypeById(new Integer(base));
		userIndent = weiboIndentService.getUserIndentById(userIndent.getId());
		User completeUser = userManagerService.findCurrectUser();
		//调用返利方法
		try {
			userManagerService.userIndentRebate(userIndent);
		} catch (Exception e) {
			e.printStackTrace();
		}
		userIndent.setState(6);
		userIndent.getRenzheng().getTencentPersonRenzheng().setState(6);
		userIndent.setEndDate(new Date());
		userIndent.setEndUser(completeUser);
		weiboIndentService.saveUserIndent(userIndent);
		return "success";
	}
	public String tencentRenzhengIndentModify(){
		baseIndentType =weiboIndentService.getBaseIndentTypeById(new Integer(base));
		Integer state = userIndent.getState();
		userIndent = weiboIndentService.getUserIndentById(userIndent.getId());
		userIndent.getRenzheng().getTencentPersonRenzheng().setState(state);
		userIndent.setState(state);
		weiboIndentService.saveUserIndent(userIndent);
		return "success";
	}
	
	public String tencentBusinessRenzhengManageUI(){
		baseIndentType =weiboIndentService.getBaseIndentTypeById(new Integer(base)); 
		
		curUser = userManagerService.findCurrectUser();
		if(currentPage>0) {
			pageNum = currentPage;
			currentPage = PaginationSupport.PAGESIZE*(currentPage-1);
		}
		if(userManagerService.isNotUser(curUser)&& !userManagerService.isUser_2(curUser)){
			paginationSupport = weiboIndentService.getIndentForPage(currentPage,true,new Integer(base));
		}else{
			paginationSupport = weiboIndentService.getIndentForPage(currentPage,false,new Integer(base));
		}
		if(qe!=null){
			current = qe.getStatus();
		}else{
			current = "all";
		}
	    
		return "success";
	}
	
	public String tencentBusinessRenzhengManageUIForSearch(){
		baseIndentType =weiboIndentService.getBaseIndentTypeById(new Integer(base)); 
		curUser = userManagerService.findCurrectUser();
		if(currentPage>0) {
			pageNum = currentPage;
			currentPage = PaginationSupport.PAGESIZE*(currentPage-1);
		}
		
		if(queryString!=null&&!queryString.isEmpty()){
			try {
				qe = weiboIndentService.paseQueryString(qe,queryString);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(userManagerService.isNotUser(curUser)&&!userManagerService.isUser_2(curUser)){
			paginationSupport = weiboIndentService.getIndentForPage(currentPage,true,new Integer(base),qe,"tencentBusinessRenzheng");
		}else{
			paginationSupport = weiboIndentService.getIndentForPage(currentPage,false,new Integer(base),qe,"tencentBusinessRenzheng");
		}
		if(qe!=null){
			current = qe.getStatus();
		}else{
			current = "all";
		}
		return "success";
	}
	
	public String toTencentBusinessRenzhengAdd(){
		if(ChildUserFilter.getFlag2()==0) {
			return "produceWeihu";
    	}
		baseIndentType = weiboIndentService.getBaseIndentTypeById(new Integer(
				base));

		if (baseIndentType.getEnable() == 0) {
			return "produceWeihu";
		}

		curUser = userManagerService.findCurrectUser();
		if (userManagerService.isRoot(curUser)) {
			price = baseIndentType.getPrice();
		} else {
			UserIndentType indentType = weiboIndentService
					.getUserIndentTypeByUserAndType(curUser,
							baseIndentType.getId());
			if (indentType == null) {
				return "403";
			} else {
				indentType = weiboIndentService.updateIndentType(indentType);
				price = indentType.getPrice();
			}
		}
		return "success";
	}
	
	public String tencentBusinessRenzhengAdd(){
		//判断价钱是否合法
				User curUser = userManagerService.findCurrectUser();
				boolean flag = weiboIndentService.isLawfulOfPrice(price,curUser,new Integer(base));
				if(flag){
					
					userIndent.setCreateDate(new Date());
					//userIndent.setNowNumber(userIndent.getStartNumber());
					User parentUser = userManagerService.findUserById(curUser.getParentUserId());
					userIndent.setParentUser(parentUser);
					userIndent.setState(0);
					
					baseIndentType =weiboIndentService.getBaseIndentTypeById(new Integer(base));
					if(userManagerService.isRoot(curUser)){
						price = baseIndentType.getPrice();
					}else{
						UserIndentType indentType = weiboIndentService.getUserIndentTypeByUserAndType(curUser,baseIndentType.getId());
						if(indentType==null){
							return "403";
						}else{
							Float oldPrice = indentType.getPrice();
							indentType = weiboIndentService.updateIndentType(indentType);
							price = indentType.getPrice();
							if(!oldPrice.equals(price)){
								return "priceChange";
							}
						}
					}
					
					userIndent.setType(new Integer(base));
					userIndent.setUser(curUser);
					Float t = price*1/baseIndentType.getUnit2();
					userIndent.setTotalPrice(t);
					userIndent.setPrice(price);
					//判余额是否充足
					Float money = curUser.getMoney();
					Float money2 = (float) Math.round(money*100/100);
					if(money2<t){
						return "moneyLow";
					}
					userIndent.setBasePrice(baseIndentType.getPrice());
					Float baseTotalPrice = baseIndentType.getPrice()*1/baseIndentType.getUnit2();
					userIndent.setBaseTotalPrice(baseTotalPrice);
					
					userIndent.setBaseIndentType(baseIndentType);
					
					TencentBusinessRenzheng tencentBusinessRenzheng = userIndent.getRenzheng().getTencentBusinessRenzheng();
					String remark = tencentBusinessRenzheng.getRemark();
					remark = Utils.replaceEnter(remark);
					tencentBusinessRenzheng.setRemark(remark);
					tencentBusinessRenzheng.setState(userIndent.getState());
					weiboIndentService.saveObject(tencentBusinessRenzheng);
					weiboIndentService.saveObject(userIndent.getRenzheng());
					userIndent.setNumber(1);
					//保存订单表
					userIndent = weiboIndentService.saveUserIndent(userIndent);
					
					
					
					// weiboIndentService.saveIndentQq(qq,userIndent);
					
					//所需金额保存到临时表
					UserIndentMoneyTmp userIndentMoneyTmp = new UserIndentMoneyTmp();
					userIndentMoneyTmp.setMoney(t);
					userIndentMoneyTmp.setUser(curUser);
					userIndentMoneyTmp.setUserIndent(userIndent);
					userIndentMoneyTmp = weiboIndentService.saveUserIndentMoneyTmp(userIndentMoneyTmp);
					//从用户账户中的总金额减去订单总价 保存
					money = curUser.getMoney();
					money = money -t;
					curUser.setMoney(money);
					UserPay userPay = userManagerService.saveUserMoneyForIndent(curUser,0,t,userIndent);
					
					weiboIndentService.getIndentDao().evict(userIndent);
					weiboIndentService.getIndentDao().evict(userIndentMoneyTmp);
					weiboIndentService.getIndentDao().evict(userPay);
					
					
					//保存返利关系
					UserIndentType indentType = weiboIndentService.getUserIndentTypeByUserAndType(curUser,baseIndentType.getId());
					weiboIndentService.saveUserIndentRebateRelation(userIndent,baseIndentType,0,indentType,curUser,price,null,null);
					//userIndent2 = userIndent;
					userIndent=null;
					
				}else{
					return "add_indent_403";
				}
			
			return "success";
	}
	
	public String tencentBusinessRenzhengIndentComplete(){
		baseIndentType =weiboIndentService.getBaseIndentTypeById(new Integer(base));
		userIndent = weiboIndentService.getUserIndentById(userIndent.getId());
		User completeUser = userManagerService.findCurrectUser();
		//调用返利方法
		try {
			userManagerService.userIndentRebate(userIndent);
		} catch (Exception e) {
			e.printStackTrace();
		}
		userIndent.setState(6);
		userIndent.getRenzheng().getTencentBusinessRenzheng().setState(6);
		userIndent.setEndDate(new Date());
		userIndent.setEndUser(completeUser);
		weiboIndentService.saveUserIndent(userIndent);
		return "success";
	}
	
	public String tencentBusinessRenzhengIndentModify(){
		baseIndentType =weiboIndentService.getBaseIndentTypeById(new Integer(base));
		Integer state = userIndent.getState();
		userIndent = weiboIndentService.getUserIndentById(userIndent.getId());
		userIndent.getRenzheng().getTencentBusinessRenzheng().setState(state);
		userIndent.setState(state);
		weiboIndentService.saveUserIndent(userIndent);
		return "success";
	}
	
	public String toTencentIndentDetails_qq_fan(){
		userIndent = weiboIndentService.getUserIndentById(userIndent.getId());
		return "success";
	}
	
	public String tencentZanManageUI(){
		baseIndentType =weiboIndentService.getBaseIndentTypeById(new Integer(base)); 
		
		curUser = userManagerService.findCurrectUser();
		if(currentPage>0) {
			pageNum = currentPage;
			currentPage = PaginationSupport.PAGESIZE*(currentPage-1);
		}
		if(userManagerService.isNotUser(curUser)&& !userManagerService.isUser_2(curUser)){
			paginationSupport = weiboIndentService.getIndentForPage(currentPage,true,new Integer(base));
		}else{
			paginationSupport = weiboIndentService.getIndentForPage(currentPage,false,new Integer(base));
		}
		if(qe!=null){
			current = qe.getStatus();
		}else{
			current = "all";
		}
	    
		return "success";
	}
	
	public String tencentZanManageUIForSearch(){
		baseIndentType =weiboIndentService.getBaseIndentTypeById(new Integer(base)); 
		curUser = userManagerService.findCurrectUser();
		if(currentPage>0) {
			pageNum = currentPage;
			currentPage = PaginationSupport.PAGESIZE*(currentPage-1);
		}
		
		if(queryString!=null&&!queryString.isEmpty()){
			try {
				qe = weiboIndentService.paseQueryString(qe,queryString);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(userManagerService.isNotUser(curUser)&&!userManagerService.isUser_2(curUser)){
			paginationSupport = weiboIndentService.getIndentForPage(currentPage,true,new Integer(base),qe,"tencentBusinessRenzheng");
		}else{
			paginationSupport = weiboIndentService.getIndentForPage(currentPage,false,new Integer(base),qe,"tencentBusinessRenzheng");
		}
		if(qe!=null){
			current = qe.getStatus();
		}else{
			current = "all";
		}
		return "success";
	}
	
	public String toTencentAddForZan(){
		if(ChildUserFilter.getFlag2()==0) {
			return "produceWeihu";
    	}
		baseIndentType = weiboIndentService.getBaseIndentTypeById(new Integer(
				base));

		if (baseIndentType.getEnable() == 0) {
			return "produceWeihu";
		}

		curUser = userManagerService.findCurrectUser();
		if (userManagerService.isRoot(curUser)) {
			price = baseIndentType.getPrice();
		} else {
			UserIndentType indentType = weiboIndentService
					.getUserIndentTypeByUserAndType(curUser,
							baseIndentType.getId());
			if (indentType == null) {
				return "403";
			} else {
				indentType = weiboIndentService.updateIndentType(indentType);
				price = indentType.getPrice();
			}
		}
		return "success";
	}
	
	public String tencentAddForZan(){
		//判断价钱是否合法
		User curUser = userManagerService.findCurrectUser();
		boolean flag = weiboIndentService.isLawfulOfPrice(price,curUser,new Integer(base));
		if(flag){
			
			userIndent.setCreateDate(new Date());
			//userIndent.setNowNumber(userIndent.getStartNumber());
			User parentUser = userManagerService.findUserById(curUser.getParentUserId());
			userIndent.setParentUser(parentUser);
			userIndent.setState(0);
			
			baseIndentType =weiboIndentService.getBaseIndentTypeById(new Integer(base));
			if(userManagerService.isRoot(curUser)){
				price = baseIndentType.getPrice();
			}else{
				UserIndentType indentType = weiboIndentService.getUserIndentTypeByUserAndType(curUser,baseIndentType.getId());
				if(indentType==null){
					return "403";
				}else{
					Float oldPrice = indentType.getPrice();
					indentType = weiboIndentService.updateIndentType(indentType);
					price = indentType.getPrice();
					if(!oldPrice.equals(price)){
						return "priceChange";
					}
				}
			}
			
			userIndent.setType(new Integer(base));
			userIndent.setUser(curUser);
			Float t = price*userIndent.getNumber()/baseIndentType.getUnit2();
			userIndent.setTotalPrice(t);
			userIndent.setPrice(price);
			//判余额是否充足
			Float money = curUser.getMoney();
			Float money2 = (float) Math.round(money*100/100);
			if(money2<t){
				return "moneyLow";
			}
			userIndent.setBasePrice(baseIndentType.getPrice());
			Float baseTotalPrice = baseIndentType.getPrice()*userIndent.getNumber()/baseIndentType.getUnit2();
			userIndent.setBaseTotalPrice(baseTotalPrice);
			userIndent.setBaseIndentType(baseIndentType);
			userIndent.setRenzheng(null);
			//保存订单表
			userIndent = weiboIndentService.saveUserIndent(userIndent);
			
			//所需金额保存到临时表
			UserIndentMoneyTmp userIndentMoneyTmp = new UserIndentMoneyTmp();
			userIndentMoneyTmp.setMoney(t);
			userIndentMoneyTmp.setUser(curUser);
			userIndentMoneyTmp.setUserIndent(userIndent);
			userIndentMoneyTmp = weiboIndentService.saveUserIndentMoneyTmp(userIndentMoneyTmp);
			//从用户账户中的总金额减去订单总价 保存
			money = curUser.getMoney();
			money = money -t;
			curUser.setMoney(money);
			UserPay userPay = userManagerService.saveUserMoneyForIndent(curUser,0,t,userIndent);
			
			weiboIndentService.getIndentDao().evict(userIndent);
			weiboIndentService.getIndentDao().evict(userIndentMoneyTmp);
			weiboIndentService.getIndentDao().evict(userPay);
			
			
			//保存返利关系
			UserIndentType indentType = weiboIndentService.getUserIndentTypeByUserAndType(curUser,baseIndentType.getId());
			weiboIndentService.saveUserIndentRebateRelation(userIndent,baseIndentType,0,indentType,curUser,price,null,null);
			//userIndent2 = userIndent;
			userIndent=null;
			
		}else{
			return "add_indent_403";
		}
	
	return "success";
	}
	
	private TencentWeixinFanStatistics txStatistics;
	
	
	
	public TencentWeixinFanStatistics getTxStatistics() {
		return txStatistics;
	}

	public void setTxStatistics(TencentWeixinFanStatistics txStatistics) {
		this.txStatistics = txStatistics;
	}

	private String wx_base;
	private List<UserBaseIndentType> wx_type_list_level3;
	
	
	
	public List<UserBaseIndentType> getWx_type_list_level3() {
		return wx_type_list_level3;
	}

	public void setWx_type_list_level3(List<UserBaseIndentType> wx_type_list_level3) {
		this.wx_type_list_level3 = wx_type_list_level3;
	}

	public String getWx_base() {
		return wx_base;
	}

	public void setWx_base(String wx_base) {
		this.wx_base = wx_base;
	}

	public String toTencentWeixinFanCreateStatistics(){
		wx_type_list_level3 = weiboIndentService.getIndentBaseTypeOfLevel3(new Integer(11));
		if(wx_base!=null) base=wx_base;
		baseIndentType =weiboIndentService.getBaseIndentTypeById(new Integer(base));
		txStatistics = new TencentWeixinFanStatistics();
		
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date now = new Date();
        String d = format.format(now);
        txStatistics.setCreateDate(d);
        
		List list = weiboIndentService.getAllIndentOfCreateByDate(txStatistics.getCreateDate(),baseIndentType);
		txStatistics.setList(list);
		txStatistics.setNumber(list.size());
		Integer n = 0;
		for(int i=0;i<list.size();i++){
			UserIndent indent = (UserIndent) list.get(i);
			n+=indent.getNumber();
		}
		txStatistics.setTotalFanNum(n);
		return "success";
	}
	
	public String tencentWeixinFanCreateStatistics(){
		wx_type_list_level3 = weiboIndentService.getIndentBaseTypeOfLevel3(new Integer(11));
		if(wx_base!=null) base=wx_base;
		baseIndentType =weiboIndentService.getBaseIndentTypeById(new Integer(base));
		if(txStatistics==null){
			return "success";
		}
		List list = weiboIndentService.getAllIndentOfCreateByDate(txStatistics.getCreateDate(),baseIndentType);
		txStatistics.setList(list);
		txStatistics.setNumber(list.size());
		Integer n = 0;
		for(int i=0;i<list.size();i++){
			UserIndent indent = (UserIndent) list.get(i);
			n+=indent.getNumber();
		}
		txStatistics.setTotalFanNum(n);
		return "success";
	}
}
